package com.banzhi.shop.activity;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.data.MyShoperData;
import com.banzhi.shop.model.CallBack.MyShoperCallBack;
import com.banzhi.shop.model.IMyShoperModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2018/6/28.
 */

public class MyShoperImpl implements IMyShoperModel {

    @Override
    public void getFindMyShoperData(String page, String keyword, final MyShoperCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.MyShopOwnerURL);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("page", page );
        params.addBodyParameter("keyword", keyword);
        LogUtils.d("MyShoperImpl-1", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        MyShoperData myShoperData = gson.fromJson(result, MyShoperData.class);
                        callBack.ongetMyShoperDataSuccess(myShoperData.getData());
                    } else {
                        callBack.ongetMyShoperDataError(jsonobject);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("MyShoperImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(95)");

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();

            }
        });
    }

    @Override
    public void getMyShoperData(String page, MyShoperCallBack callBack) {
        getFindMyShoperData(page,null,callBack);
    }

 /*  @Override
    public void getMyShoperData(String page, String keyword, final MyShoperCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.MyShopOwnerURL);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("page", page );
        if (ASUtils.isCanUse(keyword)) {
            params.addBodyParameter("keyword", keyword);
        }
        LogUtils.d("FindMyShoperData", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        MyShoperData myShoperData = gson.fromJson(result, MyShoperData.class);
                        callBack.ongetMyShoperDataSuccess(myShoperData.getData());

                    } else {
                        callBack.ongetMyShoperDataError(jsonObject);

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("MyShoperImpl-2", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(97)");


            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();

            }
        });

    }*/
}
